package com.quanyan.travel.component.task;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.quanyan.travel.service.TravelAdminService;
import com.quanyan.travel.service.TravelOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xiaoxiao on 16/5/31.
 */
//@Component
public class ScheduledTask {

    private static Logger logger = LoggerFactory.getLogger(ScheduledTask.class);
    private Integer refreshStorageCount = 1;

    @Autowired
    private TravelOrderService travelOrderService;


    @Autowired
    private TravelAdminService travelAdminService;

    /**
     * 每隔一定时间来刷新仓库,关闭未进行支付的订单
     *
     * */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void reportCurrentTime() throws InterruptedException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(String.format("回收不合法订单--->>>第%s次执行，当前时间为：%s", refreshStorageCount++, dateFormat.format(new Date())));
        try {
            travelOrderService.refreshTravelStorage();
        }catch (Exception e){
            logger.error("回收不合法订单--->>>第%s次执行,出错: {}", e);
        }
    }


    /**
     * 每隔一定时间来更新活动的状态,定时刷新已经下架的非季度活动,同时下架非季度活动下的场次
     *
     * */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void refreshTravelActivityStatus() throws InterruptedException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(String.format("清除过期活动--->>>第%s次执行，当前时间为：%s", refreshStorageCount++, dateFormat.format(new Date())));
        try {
            /**
             * 清除activiey表过期季度活动，非季度活动
             * 清除theme表过期季度活动
             * 清除product表过期场次
             * 将报名时间结束的活动改为首页不显示状态
             */
            travelAdminService.refreshTravelActivityStatus();
        }catch (Exception e){
            logger.error("清除过期活动--->>>第%s次执行,出错: {}", e);
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void updateLowPrice() throws InterruptedException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(String.format("更新最低价格和开始时间--->>>第%s次执行，当前时间为：%s", refreshStorageCount++, dateFormat.format(new Date())));
        try {
            travelAdminService.updateLowPrice();
        }catch (Exception e){
            logger.error("更新最低价格和开始时间--->>>第%s次执行,出错: {}", e);
        }
    }


    /**
     * 每隔分钟刷新一下提醒用户付款
     *
     * */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void userPayRemind() throws InterruptedException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(String.format("支付提醒--->>>第%s次执行，当前时间为：%s", refreshStorageCount++, dateFormat.format(new Date())));
        try {
            travelAdminService.remindUserPay();
        }catch (Exception e){
            logger.error("支付提醒--->>>第%s次执行,出错: {}", e);
        }
    }




}
